﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace One.Framework.Util
{
    /// <summary>
    /// 날짜시간
    /// </summary>
    public class DateHelper
    {
        /// <summary>
        /// 날짜형 yyyyMMdd 형 문자열 변환 
        /// </summary>
        /// <param name="date">DateTime 개체</param>
        /// <returns>yyyyMMdd형 문자열 반환</returns>
        public static string ConvertDateToChar8(DateTime date)
        {
            return date.ToString("yyyyMMdd");
        }

        /// <summary>
        /// yyyy-mm-dd 날짜형식의 문자열을 yyyyMMdd형 문자열로 변환
        /// </summary>
        /// <param name="date">yyyy-mm-dd 날짜형식 문자열</param>
        /// <returns>yyyyMMdd형 문자열</returns>
        public static string ConvertDateToChar8(String date)
        {
            string returnString = "";
            if (date.Length >= 8 && !date.Equals("____-__-__"))
            {
                returnString = Convert.ToDateTime(date).ToString("yyyyMMdd");
            }

            return returnString;
        }

        /// <summary>
        /// yyyyMMdd문자열을 yyyy-MM-dd형 문자열로 변환
        /// </summary>
        /// <param name="data">yyyyMMdd형 문자열</param>
        /// <returns>yyyy-MM-dd형 문자열 반환</returns>
        public static string ConvertChar8ToDate(String data)
        {
            if (String.IsNullOrEmpty(data))
                return "";

            DateTime d = new DateTime(
            Convert.ToInt32(data.Substring(0, 4)),
            Convert.ToInt32(data.Substring(4, 2)),
            Convert.ToInt32(data.Substring(6, 2))
            );

            return d.ToShortDateString();
        }

        /// <summary>
        /// yyyyMMdd문자열을 yyyy-MM-dd형 문자열로 변환
        /// </summary>
        /// <param name="dateString">yyyyMMdd형 문자열</param>
        /// <returns>yyyy-MM-dd형 문자열 반환</returns>
        public static string ConvertChar8ToDateString(string dateString)
        {
            string newDateString = "";

            if (dateString.Length == 8)
            {
                newDateString = dateString.Substring(0, 4).ToString() + "-" + dateString.Substring(4, 2).ToString() + "-" + dateString.Substring(6, 2).ToString();
            }

            return newDateString;
        }

        /// <summary>
        /// 시간정보바인딩
        /// </summary>
        /// <returns></returns>
        public static string[] BindHourString()
        {
            string[] hours = new string[24];
            string temp = String.Empty;
            int startIndex = 0;

            for (int i = 0; i < 24; i++)
            {
                temp = "0" + i.ToString();
                if (temp.Length > 2) { startIndex = 1; }
                hours[i] = temp.Substring(startIndex, 2).ToString();
            }

            return hours;
        }

        /// <summary>
        /// 시간정보바인딩(시작종료시간지정가능)
        /// </summary>
        /// <param name="startHour">시작시간</param>
        /// <param name="endHour">종료시간</param>
        /// <returns>시간배열</returns>
        public static string[] BindHourString(int startHour, int endHour)
        {
            int hourGab = endHour - startHour;
            string[] hours = new string[hourGab + 1];
            string temp = String.Empty;
            int startIndex = 0;

            int arryIndex = 0;

            for (int i = startHour; i <= endHour; i++)
            {
                temp = "0" + i.ToString();
                if (temp.Length > 2) { startIndex = 1; }
                hours[arryIndex] = temp.Substring(startIndex, 2).ToString();

                arryIndex = arryIndex + 1;
            }

            return hours;
        }

        /// <summary>
        /// 분정보 바인딩
        /// </summary>
        /// <returns></returns>
        public static string[] BindMinuteString()
        {
            string[] minutes = new string[60];
            string temp = String.Empty;
            int startIndex = 0;

            for (int i = 0; i < 60; i++)
            {
                temp = "0" + i.ToString();
                if (temp.Length > 2) { startIndex = 1; }
                minutes[i] = temp.Substring(startIndex, 2).ToString();
            }

            return minutes;
        }

        /// <summary>
        /// 협정 세계시(Universal Time Coordinated)
        /// </summary>
        /// <param name="dateTime">DateTime</param>
        /// <returns></returns>
        public static DateTime ToUtcTime(DateTime dateTime)
        {
            return dateTime.ToUniversalTime();
        }

        /// <summary>
        /// 협정 세계시(Universal Time Coordinated)
        /// </summary>
        /// <param name="dateTime">DateTime</param>
        /// <returns></returns>
        public static string ToUtcTimeString(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("ddd, d MMM yyyy HH:mm:ss ") + "GMT";
        }

        /// <summary>
        /// 협정 세계시(Universal Time Coordinated)
        /// </summary>
        /// <param name="dateTime">DateTime</param>
        /// <returns></returns>
        public static string UtcTimeToChar8(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("yyyyMMdd");
        }

        /// <summary>
        /// yyyy-mm-dd 날짜형식의 문자열을 협정 세계시(Universal Time Coordinated) yyyyMMdd형 문자열로 변환 
        /// </summary>
        /// <param name="date">yyyy-mm-dd 날짜형식 문자열</param>
        /// <returns></returns>
        public static string UtcTimeToChar8(string date)
        {
            return Convert.ToDateTime(date).ToUniversalTime().ToString("yyyyMMdd");
        }
    }
}
